<?php
/**
 * Created by PhpStorm.
 * User: Ruzi
 * Date: 2015/12/10
 * Time: 11:10
 */

header("Content-type: application/json; charset=utf-8");
$arr=array();
//echo "操作系统".PHP_OS."<br>";
//echo "操作系统语言".$_SERVER['HTTP_ACCEPT_LANGUAGE'];
//exec('ipconfig/all', $arr);   //for windows

//exec('cat /etc/sysconfig/network-scripts/ifcfg-em1', $arr);   //for linux
//print_r($arr);
if(strstr(PHP_OS,"WIN")){
    //windows system
    exec('ipconfig/all', $arr);   //for windows
    $data=array();
    $wg=array();
    for($i=0;$i<count($arr);$i++){
        $l="";
       $l=iconv("GBK","UTF-8",$arr[$i]);
       // $l=$arr[$i];


        //如果系统语言是中文
        if(strrpos(strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']), 'zh-cn') !== false) {
         //   echo '<br />你的是中文操作系统<br />';


            if (trim(strstr($l, "默认网关"))!="") {
                $s1 = strstr($l, "默认网关");
                $s2 = str_replace("默认网关", "", $s1);
                $s3 = str_replace(". .", "", $s2);
                $s4 = str_replace(":", "", $s3);
                $s5 = str_replace(". ", "", $s4);

                if (trim($s5) != "") {

                   // $arr_r = array("操作系统" => PHP_OS, "服务器地址" => $_SERVER["SERVER_ADDR"], "服务器网关" => trim($s5));

                    array_push($wg,trim($s5));

                  //  array_push($data, $arr_r);


                }
            }
        }



    }

    $ar1=array_unique($wg);

    for($i=0;$i<count($ar1);$i++) {

        $arr_r = array("success"=>true,"操作系统" => PHP_OS, "服务器地址" => $_SERVER["SERVER_ADDR"], "服务器网关" => trim($ar1[$i]));

        array_push($data, $arr_r);

    }



    echo json_encode($data);



}else if(strstr(PHP_OS,"linux")){

    //linux system
    echo "linux";
    exec('cat /etc/sysconfig/network-scripts/ifcfg-em1', $arr);   //for linux
    print_r($arr);
}







